<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>Preview</title>
</head>
<body>
<script>
    // 捕获错误并发送到父窗口
    window.addEventListener('error', (e) => {
        window.parent.postMessage({type: 'ERROR', message: e.message})
    })

    // 重写console方法，捕获所有console输出并发送到父窗口
    const originalConsole = {
        log: console.log,
        info: console.info,
        warn: console.warn,
        error: console.error
    };

    // 重写console.log
    console.log = function() {
        originalConsole.log.apply(console, arguments);
        const content = Array.from(arguments).map(arg => {
            try {
                return typeof arg === 'object' ? JSON.stringify(arg) : String(arg);
            } catch (e) {
                return String(arg);
            }
        }).join(' ');
        window.parent.postMessage({type: 'CONSOLE', logType: 'log', content}, '*');
    };

    // 重写console.info
    console.info = function() {
        originalConsole.info.apply(console, arguments);
        const content = Array.from(arguments).map(arg => {
            try {
                return typeof arg === 'object' ? JSON.stringify(arg) : String(arg);
            } catch (e) {
                return String(arg);
            }
        }).join(' ');
        window.parent.postMessage({type: 'CONSOLE', logType: 'info', content}, '*');
    };

    // 重写console.warn
    console.warn = function() {
        originalConsole.warn.apply(console, arguments);
        const content = Array.from(arguments).map(arg => {
            try {
                return typeof arg === 'object' ? JSON.stringify(arg) : String(arg);
            } catch (e) {
                return String(arg);
            }
        }).join(' ');
        window.parent.postMessage({type: 'CONSOLE', logType: 'warn', content}, '*');
    };

    // 重写console.error
    console.error = function() {
        originalConsole.error.apply(console, arguments);
        const content = Array.from(arguments).map(arg => {
            try {
                return typeof arg === 'object' ? JSON.stringify(arg) : String(arg);
            } catch (e) {
                return String(arg);
            }
        }).join(' ');
        window.parent.postMessage({type: 'CONSOLE', logType: 'error', content}, '*');
    };
</script>
<script type="importmap"></script>
<script type="module" id="appSrc"></script>
<div id="root"></div>
</body>
</html>
